<template>
  <div class="l-rblock l-form-viewer learun-el-form-left">
    <el-form
      :model="formData"
      :rules="rules"
      size="mini"
      labelPosition="left"
      labelWidth="110px"
      ref="form"
    >
      <div class="l-auto-window only-tabs">
        <el-tabs v-model="activeName" type="card">
          <el-tab-pane
            class="l-tabs-container"
            :label="$t('主表信息')"
            name="tab0"
          >
            <el-row :gutter="16">
              <div class="l-rblock pd-16">
                <div class="flex flex-center flex-c-between">
                  <div style="margin-bottom: 0" class="learun-form-title">
                    {{ $t("选择物料") }}
                  </div>
                  <el-button
                    v-if="!isEdit"
                    size="mini"
                    type="primary"
                    @click="materialBtn"
                    >{{ $t("选择物料") }}</el-button
                  >
                </div>
                <div class="mt-16">
                  <el-form-item
                    :label="$t('物料编号')"
                    prop="case_erp_supplymaterial_f_Number"
                  >
                    <el-input
                      size="mini"
                      disabled
                      v-model="formData.case_erp_supplymaterial_f_Number"
                      :placeholder="$t('please select')"
                    />
                  </el-form-item>
                  <el-form-item
                    :label="$t('物料名称')"
                    prop="case_erp_supplymaterial_f_Name"
                  >
                    <el-input
                      :readonly="true"
                      v-model="formData.case_erp_supplymaterial_f_Name"
                      :placeholder="$t('please select')"
                    >
                    </el-input>
                  </el-form-item>
                  <el-form-item
                    :label="$t('规格型号')"
                    prop="case_erp_supplymaterial_f_Model"
                  >
                    <el-input
                      :readonly="true"
                      v-model="formData.case_erp_supplymaterial_f_Model"
                      :placeholder="$t('please select')"
                    >
                    </el-input>
                  </el-form-item>
                </div>
                <div class="learun-form-title mt-16">{{ $t("其他设置") }}</div>

                <div>
                  <el-form-item
                    :label="$t('当前采购单价')"
                    prop="case_erp_supplymaterial_f_Price"
                  >
                    <el-input
                      v-model="formData.case_erp_supplymaterial_f_Price"
                      :placeholder="$t('please enter')"
                    >
                    </el-input>
                  </el-form-item>
                  <el-form-item
                    :label="$t('状态')"
                    prop="case_erp_supplymaterial_f_State"
                  >
                    <el-switch
                      :activeValue="1"
                      :inactiveValue="0"
                      v-model="formData.case_erp_supplymaterial_f_State"
                    />
                  </el-form-item>
                </div>
              </div>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </div>
    </el-form>

    <!-- 选择物料 -->
    <l-dialog
      :title="$t('选择物料')"
      :visible.sync="materialVisible"
      :height="600"
      :width="1000"
      @ok="handleSelect"
    >
      <l-choose-material ref="choseForm" :multiple="false" />
    </l-dialog>
  </div>
</template>
<script>
const api = window.$api.erpcase.supplier;
export default {
  props: {
    isReadOnly: {
      type: Boolean,
      default: false,
    },
    supplierId: String,
  },
  data() {
    return {
      materialVisible: false, // 弹窗
      isEdit: false,
      moduleCode: "erpCase_supplymaterial",
      activeName: "tab0",
      formData: {
        case_erp_supplymaterial_f_Number: "", // 物料编号
        case_erp_supplymaterial_f_Name: "", // 物料名称
        case_erp_supplymaterial_f_Model: "", // 规格型号
        case_erp_supplymaterial_f_Price: "", // 当前采购单价
        case_erp_supplymaterial_f_State: 1, // 状态
      },
      rules: {
        case_erp_supplymaterial_f_Number: [
          { required: true, message: "please select物料" },
        ],
        case_erp_supplymaterial_f_Name: [
          { required: true, message: "please select物料" },
        ],
        case_erp_supplymaterial_f_Model: [
          { required: true, message: "please select物料" },
        ],
        case_erp_supplymaterial_f_Price: [
          { required: true, message: "please enter采购单价" },
          { pattern: /^-?[0-9]+$/, message: "please enter数字" },
        ],
      },
    };
  },
  computed: {},
  methods: {
    // 赋值
    async handleSelect() {
      const data = await this.$refs.choseForm.getSelectData();

      this.formData.case_erp_supplymaterial_f_Number = data[0].f_Number;
      this.formData.case_erp_supplymaterial_f_Name = data[0].f_Name;
      this.formData.case_erp_supplymaterial_f_Model = data[0].f_Model;
      this.formData.case_erp_supplymaterial_f_Unit = data[0].f_Unit;
      this.$refs.choseForm.resetForm();
      this.materialVisible = false;
    },

    // 选择物料
    materialBtn() {
      this.materialVisible = true;
    },

    // Reset表单
    resetForm() {
      this.$formClear(this.$refs.form);
      this.formData.case_erp_supplymaterial_f_Id = "";
      this.isEdit = false;
      this.activeName = "tab0";
    },
    // 校验表单
    async validateForm() {
      if (!(await this.$formValidateWraper(this.$refs.form))) {
        return false;
      }

      return true;
    },
    // 设置表单数据
    setForm(data) {
      this.$setFormData(this.formData, data, "case_erp_supplymaterial");
    },
    // 获取表单数据
    getForm() {
      return this.$getFormData(this.formData, "case_erp_supplymaterial");
    },

    async loadFormData({ keyValue /*,params,node*/ }) {
      this.isEdit = true;
      //  keyValue 流程中相当于流程processId,params 传递参数,node 流程节点
      const data = await this.$awaitWraper(api.oneMaterial(keyValue));
      if (data) {
        this.setForm(data);
        return true;
      } else {
        return false;
      }
    },

    async saveForm({ keyValue, isEdit /*,code,node*/ }) {
      // isEdit 是否更新数据, keyValue 流程中相当于流程processId,code 表示流程中的操作码,node 流程节点
      const postData = this.getForm();
      postData.f_SupplierId = this.supplierId;
      if (isEdit) {
        // Edit
        return await this.$awaitWraper(api.updateMaterial(keyValue, postData));
      } else {
        // Add
        return await this.$awaitWraper(api.addMaterial(postData));
      }
    },
  },
};
</script>